<template>
  <el-container>
    <my-theme-card>
      <el-header>资源管理</el-header>
      <el-main>
        <el-col :span="1"><div class="grid-content bg-purple-light">
          <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
        </div></el-col>
        <template>
          <el-table :data="sourceList" stripe border style="width: 100%">
            <el-table-column prop="name" label="名称" width="180" />
            <el-table-column label="图片">
              <template slot-scope="scope">
                <img v-if="scope.row.icon" :src="baseUrl+scope.row.icon" style="width:50px;height:50px;">
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="180">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
              </template>
            </el-table-column>

          </el-table>
        </template>
      </el-main>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <el-dialog :title="form.title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" center append-to-body>
        <el-form :model="tempSeries" label-width="130px" :inline="true">
          <el-form-item label="名称">
            <el-input v-model="tempSeries.name" />
          </el-form-item>
          <el-form-item label="图片">
            <el-input v-model="tempSeries.icon" @focus="chooseSourceClick" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addTask">确 定</el-button>
        </div>
      </el-dialog>

      <my-delete-dialog
        :show-dialog="dialogConfirmVisible"
        :row="currentSeries"
        @deleteTask="deleteTask"
        @cancelTask="dialogConfirmVisible = false"
      />

      <el-dialog :title="form.sourceChooseTitle" :visible.sync="dialogSourcesVisible" :close-on-click-modal="false" center :custom-class="dialog1" append-to-body>
        <source-list @chooseSourceHandle="closeSourceDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>
<script>
import request from '@/utils/request'
const sourceList = () => import('@/components/SourceList/index')
export default {
  components: {
    sourceList
  },
  data: function() {
    return {
      dialog1: 'dialog1',
      deleteMessage: '确认删除该条记录吗？',
      tempSeries: {},
      baseUrl: process.env.VUE_APP_BASE_API,
      form: {
        title: '资源添加',
        sourceChooseTitle: '资源选择'
      },
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      sourceList: [],
      currentSeries: {},
      dialogFormVisible: false,
      dialogSourcesVisible: false,
      dialogConfirmVisible: false
    }
  },
  created() {
    this.query()
  },
  methods: {
    chooseSourceClick: function() {
      this.dialogSourcesVisible = true
    },
    // 关闭
    closeSourceDialog: function(ids, [{ url }]) {
      this.tempSeries.icon = url
      this.dialogSourcesVisible = false
    },
    // 新增属性任务
    addTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/series/add',
        method: 'post',
        data: this.tempSeries
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.tempSeries = {}
          this.dialogFormVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    deleteClick: function(row) {
      this.currentSeries = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/series/delete',
        method: 'post',
        data: this.currentSeries
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 新增资源点击事件
    addClick: function() {
      this.dialogFormVisible = true
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    query: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/series/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.sourceList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
